package com.xiaoyu.lanling.feature.moment.controller.my

import `in`.srain.cube.views.list.CubeRecyclerViewSimpleAdapter
import `in`.srain.cube.views.list.PagedListDataModelWithRequestTag
import android.content.Context
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.xiaoyu.base.event.AppEventBus
import com.xiaoyu.base.event.SimpleEventHandler
import com.xiaoyu.base.view.list.BaseListViewController
import com.xiaoyu.lanling.event.moment.DeleteMomentEvent
import com.xiaoyu.lanling.event.moment.MyMomentListEvent
import com.xiaoyu.lanling.event.moment.detail.CommentMomentEvent
import com.xiaoyu.lanling.event.moment.detail.DeleteCommentEvent
import com.xiaoyu.lanling.event.moment.detail.LikeMomentEvent
import com.xiaoyu.lanling.feature.moment.model.MomentItem
import com.xiaoyu.lanling.feature.moment.viewholder.MyMomentViewHolder
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class MyMomentController(context: Context, listData: PagedListDataModelWithRequestTag<MomentItem, Any>, adapter: CubeRecyclerViewSimpleAdapter<MomentItem>, swipeRefreshLayout: SwipeRefreshLayout, recyclerView: RecyclerView) : BaseListViewController<MomentItem, Any>(context, listData, adapter, swipeRefreshLayout, recyclerView) {

    companion object {

        fun create(context: Context, listData: PagedListDataModelWithRequestTag<MomentItem, Any>, swipeRefreshLayout: SwipeRefreshLayout, recyclerView: RecyclerView): MyMomentController {
            val adapter = CubeRecyclerViewSimpleAdapter<MomentItem>()
            adapter.setViewHolderClass(0, null, MyMomentViewHolder::class.java, 20)

            val controller = MyMomentController(context, listData, adapter, swipeRefreshLayout, recyclerView)

            AppEventBus.bindContainerAndHandler(context, object : SimpleEventHandler() {

                @Subscribe(threadMode = ThreadMode.MAIN)
                fun onEvent(event: MyMomentListEvent) {
                    controller.onListEvent(event)
                }

                @Subscribe(threadMode = ThreadMode.MAIN)
                fun onEvent(event: DeleteMomentEvent) {
                    val index = listData.listPageInfo.dataList.indexOfFirst { it.feed.id == event.fid }
                    if (index != -1) {
                        listData.listPageInfo.dataList.removeAt(index)
                        adapter.notifyItemRemoved(index)
                    }
                }

                @Subscribe(threadMode = ThreadMode.MAIN)
                fun onEvent(event: DeleteCommentEvent) {
                    val index = listData.listPageInfo.dataList.indexOfFirst { it.feed.id == event.fid }
                    if (index != -1) {
                        listData.listPageInfo.dataList[index].feed.commentCount--
                        adapter.notifyItemChanged(index)
                    }
                }

                @Subscribe(threadMode = ThreadMode.MAIN)
                fun onEvent(event: CommentMomentEvent) {
                    val index = listData.listPageInfo.dataList.indexOfFirst { it.feed.id == event.fid }
                    if (index != -1) {
                        val momentItem = listData.listPageInfo.dataList[index]
                        momentItem.feed.commentCount += 1
                        adapter.notifyItemChanged(index)
                    }
                }

                @Subscribe(threadMode = ThreadMode.MAIN)
                fun onEvent(event: LikeMomentEvent) {
                    val index = listData.listPageInfo.dataList.indexOfFirst { it.feed.id == event.fid && it.user.uid == event.uid }
                    if (index != -1) {
                        val momentItem = listData.listPageInfo.dataList[index]
                        momentItem.feed.liked = momentItem.feed.liked.not()
                        momentItem.feed.likeCount += if (momentItem.feed.liked) 1 else -1
                        adapter.notifyItemChanged(index)
                    }
                }
            })

            return controller
        }
    }
}